package com.ynny.librarysystemmanager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ynny.librarysystemmanager.entity.User;
import com.ynny.librarysystemmanager.entity.UserDTO;
import com.ynny.librarysystemmanager.mapper.UserMapper;
import com.ynny.librarysystemmanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll(User user) {

        return userMapper.selectAll(user);

    }

    @Override
    public User findOne(Integer userId) {
        return userMapper.selectOne(userId);
    }

    @Override
    public int addUser(User user) {

        return userMapper.insert(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.update(user);
    }

    @Override
    public int disableUser(Integer userId , Integer status) {
        return userMapper.delete(userId , status);
    }



//    @Override
//    public boolean login(String username, String password) {
//        //1.用户名是否存在
//        User user = new User();
//        user.setUsername(username);
//        List<User> users = userMapper.selectAll(user);
//        if (! (users != null && users.size() > 0 )){
//            return false;
//        }
//        //2.密码是否正确
//        user.setPassword(password);
//        List<User> usersByUserNameAndPwd = userMapper.selectAll(user);
//        if (! (usersByUserNameAndPwd != null && usersByUserNameAndPwd.size() > 0 )){
//            return true;
//        }
//
//        return false;
//
//    }

    @Override
    public User login(String username, String password) {

        User user = new User();
        user.setUsername(username);


        User userByUserName = userMapper.selectUser(user);

        if (userByUserName == null){
            return null;
        }

        user.setPassword(password);
        User userByUserNameAndPwd = userMapper.selectUser(user);

        if (userByUserNameAndPwd == null){
            return null;
        }


        return userByUserNameAndPwd;

    }



    @Override
    public boolean login(User user) {
        return false;
    }

    @Override
    public PageInfo page(UserDTO dto) {

        PageHelper.startPage(dto.getPageNum() , dto.getPageSize());

        List<User> users = userMapper.selectAll(dto);

        PageInfo<User> userPageInfo = new PageInfo<>(users);

        return userPageInfo;

    }


}
